/**
 * Created by cxg on 2017/4/26.
 */
//创建数据库

import {
    AppRegistry,
    Text,
    Platform,
    View,
    StyleSheet,
    Alert
} from 'react-native';
import Constant from '../constant/constant.js';
import DbUtil from './db-util.js';

var DeviceInfo = require('react-native-device-info');

class InitDB {
    version = Constant.version;

    static init() {
        let db = DbUtil.open();
        //建表
        DbUtil.createTable(db);
        //手机信息
        let deviceInfo = InitDB.getDeviceInfo();
        db.transaction((tx) => {
            tx.executeSql('DELETE FROM APP_INFO');
            tx.executeSql('INSERT INTO APP_INFO(VERSION,OS,OS_VERSION,MOBILE_BRAND,IMEI) VALUES(?,?,?,?,?)',
                [Constant.version + '', deviceInfo.os + "", deviceInfo.osVersion + '', deviceInfo.mobileBrand + '', deviceInfo.imei + '']
                , () => {
                }, (error) => {
                    DbUtil._errorCB(error);
                });
        }, (error) => {
            DbUtil._errorCB(error);
        }, () => {
        })
        DbUtil.close(db);
    }

    static getDeviceInfo() {
        let systemVersion = DeviceInfo.systemVersion;
        if (!systemVersion) {
            systemVersion = '未知';
        }
        return {
            os: '' + Platform.OS,
            osVersion: systemVersion,
            platform: '' + DeviceInfo.model,
            mobileBrand: '' + DeviceInfo.brand,
            imei: '' + DeviceInfo.uniqueID
        }
    }


    static loginDataToDB(json) {
        let db = DbUtil.open();
        db.transaction((tx) => {
            // Create a book object
            if (json.empid) {
                tx.executeSql('DELETE CURRENT_USER');
                tx.executeSql('INSERT INTO CURRENT_USER(username,empid) VALUES(?,?)', [json.empid, json.empid]);
            }
            if (json.course_user) {
                if (json.course_user === '-1') {
                    tx.executeSql('DELETE COURSE_USER WHERE empId=?', [json.empid]);
                } else {
                    tx.executeSql('DELETE COURSE_USER WHERE empId=?', [json.empid]);

                    json.course_user.map(function (item, i) {

                        tx.executeSql(
                            'INSERT INTO COURSE_USER(ID_,empId,COURSE_ID) VALUES(?,?,?)',
                            [item.id_, item.empId, item.course_id]);
                    });
                }
            }
            if (json.cacheDataVersion) {
                if (json.cacheDataVersion === '-1') {
                    tx.executeSql('DELETE cacheDataVersion ');
                } else {
                    tx.executeSql('DELETE cacheDataVersion ');

                    let cacheDataVersion = json.cacheDataVersion;
                    for (var prop in json.cacheDataVersion) {
                        if (json.cacheDataVersion.hasOwnProperty(prop)) {
                            tx.executeSql('INSERT INTO cacheDataVersion (obj,empid,hashCode_) values(?,?,?) ',
                                [prop, json.empid, cacheDataVersion[prop] + ""]
                            );
                        }
                    }
                }
            }

            if (json.banner) {
                if (json.banner === '-1') {
                    tx.executeSql('DELETE BANNER ');
                } else {
                    tx.executeSql('DELETE BANNER ');

                    json.banner.map(function (item, i) {
                        tx.executeSql('INSERT INTO BANNER (ID_,SEQ_,URL_) values(?,?,?) ',
                            [item.id_, item.seq_, item.url_]
                        );
                    });
                }
            }

            if (json.shequnsiweiri_image) {
                if (json.shequnsiweiri_image === '-1') {
                    tx.executeSql('DELETE SHEQUNSIWEIRI_IMAGE ');
                } else {
                    tx.executeSql('DELETE SHEQUNSIWEIRI_IMAGE ');
                    json.shequnsiweiri_image.map(function (item, i) {
                        tx.executeSql('INSERT INTO SHEQUNSIWEIRI_IMAGE (ID_,IMAGE_) values(?,?) ',
                            [item.id_, item.image_]
                        );
                        //  ToastUtil.show('image:'+item.image_)
                    });
                }
            }

            if (json.course) {
                if (json.course === '-1') {
                    tx.executeSql('DELETE VOICES ');
                    tx.executeSql('DELETE COURSE ');
                } else {
                    tx.executeSql('DELETE VOICES ');
                    tx.executeSql('DELETE COURSE ');
                    json.course.map(function (item, i) {
                        tx.executeSql('INSERT INTO COURSE (ID_,AUTHOR_DESC,COLUMN_DESC,SUITABLE_FOR,price_) values(?,?,?,?,?) ',
                            [item.id_, item.author_desc_, item.column_desc, item.suitable_for, item.price_]
                        );
                        item.voices.map(function (item2, i) {
                            tx.executeSql('INSERT INTO VOICES (ID_,COURSE_ID,FREE_,IMAGE_,TITLE1,TITLE2,URL_) values(?,?,?,?,?,?,?,?) ',
                                [item2.id_,
                                    item2.course_id,
                                    item2.free_,
                                    item2.image_,
                                    item2.title1,
                                    item2.title2,
                                    "" + item2.url_]
                            );
                        });
                    });
                }
            }
        });
        DbUtil.close(db);
    }

    static getCurrentUser() {
        let app_info_record = {}
        let db = DbUtil.open();
        db.transaction((tx) => {
            tx.executeSql('select * from CURRENT_USER'
                , []
                , (tx2, result) => {
                    app_info_record.username = result.rows.item(0).username;
                    app_info_record.mobile = app_info_record.username;
                });
        })
        DbUtil.close(db);
        app_info_record = {};
        return app_info_record;
    }
}

module.exports = InitDB;